exports.installAggregationExtension = function (req, res) {

    //获取参数信息
    var bodyParams = req.routeInfo.bodyParams;

    //返回结果
    var resultData = {};

    //任务流创建
    async.waterfall([

        //判断该模块是否已安装过
        (cb) => {

            //查询是否已安装过该模块
            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.sysOrganizationalFunctionModuleSearch.Request_Api_Url + "/fm_fmid/" + bodyParams.fm_fmid,
                    method : 'GET'
                },
                function (data) {

                    var moduleData = data.result.data[0];

                    //判断是否安装
                    if(moduleData.fm_status == 1){

                        return RES.errorResponse(res, false, "您已安装过该模块，请勿重复安装");

                    }

                    cb();
                
                }
            )

        },

        //请求核心层功能扩展接口
        (cb)=> {

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.extensionApiInstallExtension.Request_Api_Url,
                    method : 'POST',
                    data : {
                        fm_id          : bodyParams.fm_id,
                        fm_name        : bodyParams.fm_name,
                        fm_install_dir : bodyParams.fm_install_dir,
                        fm_dir_name    : bodyParams.fm_dir_name
                    }
                },
                function (data) {

                    resultData.actionResult = data.result;

                    cb();
                   
                }
            )

        },

        //下载聚合层扩展接口配置文件
        (cb) => {

            var src = bodyParams.fm_install_dir + "/aggregation/" + bodyParams.fm_dir_name + ".api.js";
            var dsc = "./extension/api/extensionApi/" + bodyParams.fm_dir_name + ".api.js";

            common.downLoadFile(src,dsc,function(rs,error){

                if(rs){
                    resultData.actionResult['下载聚合层扩展接口配置文件'] = "[成功]";
                    cb();
                }else{
                    return RES.errorResponse(res, false, error);
                }

            });

        },

        //下载聚合层扩展原子层接口配置文件
        (cb) => {

            var src = bodyParams.fm_install_dir + "/aggregation/" + bodyParams.fm_dir_name + ".core.js";
            var dsc = "./extension/api/extensionCoreApi/" + bodyParams.fm_dir_name + ".core.js";

            common.downLoadFile(src,dsc,function(rs,error){

                if(rs){
                    resultData.actionResult['下载聚合层扩展原子层接口配置文件'] = "[成功]";
                    cb();
                }else{
                    return RES.errorResponse(res, false, error);
                }

            });

        },

        //下载扩展接口处理文件，并解压至路由目录
        (cb) => {

            var src = bodyParams.fm_install_dir + "/aggregation/aggregation.zip";
            var dsc = "./extension/routes/aggregation.zip";

            common.downLoadFile(src,dsc,function(rs,error){

                if(rs){

                    common.unZipAction(dsc,'./extension/routes/',function(){

                        common.unlinkFile(dsc,function(unlinkRs){

                            resultData.actionResult['下载扩展接口处理文件，并解压至路由目录'] = "[成功]";
                        
                            cb();

                        })
                    })

                }else{
                    return RES.errorResponse(res, false, error);
                }

            });
        },

        //更新安装状态
        (cb) => {

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.sysOrganizationalFunctionModuleUpdate.Request_Api_Url,
                    method : 'POST',
                    data : {
                        update_where  : "fm_id",
                        fm_id         : bodyParams.fm_id,
                        fm_status     : 1
                    }
                },
                function (data) {

                    resultData.actionResult['更新扩展模块安装状态'] = "[成功]";

                    cb()
                   
                }
            )

        }

    ],()=> {

        //返回最终拼接结果
        RES.successResponse(res, resultData);

    });

}